package com.mtnz.njk.Controller;

import com.mtnz.njk.Service.CartService;
import com.mtnz.njk.Service.IntegralService;
import com.mtnz.njk.Service.ProductService;
import com.mtnz.njk.Service.RemarkService;
import com.mtnz.njk.entity.ApplyVipEntity;
import com.mtnz.njk.entity.CartProductEntity;
import com.mtnz.njk.entity.OrderInfoEntity;
import com.mtnz.njk.entity.ProductEntity;
import com.mtnz.njk.model.AddUserCartModel;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 商品信息控制层
 * @Author: FBL
 * @CreateDate: 2019\7\9 0009 8:51
 * @Version: 1.0
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    RemarkService remarkService;

    @Autowired
    IntegralService integralService;

    @Autowired
    ProductService productService;

    @Autowired
    CartService cartService;

    /**
     * 查询商城轮播图
     *
     * @return
     */
    @PostMapping("/listBanner")
    public ResponseResult listBanner() throws Exception {
        return productService.listBanner();
    }

    /**
     * 展示商品列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/showListProduct")
    public ResponseResult showListProduct(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) throws Exception {
        return productService.showListProduct(pageNum, pageSize);
    }

    /**
     * 商品搜索
     *
     * @param productName
     * @return
     */
    @PostMapping("/searchProduct")
    public ResponseResult searchProduct(String productName) throws Exception {
        return productService.searchProduct(productName);
    }


    /**
     * 查询商品详情
     *
     * @param userId
     * @param productId
     * @return
     * @throws Exception
     */
    @PostMapping("/productDetail")
    public ResponseResult productDetail(String userId, String productId) throws Exception {
        if (userId == null || ("").equals(userId) || productId == null || ("").equals(productId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        ProductEntity productEntity = productService.productDetail(userId, productId);
        return new ResponseResult(ResponseMessage.OK, productEntity);
    }

    /**
     * 查询套餐商品详情
     *
     * @param userId
     * @param productId
     * @return
     * @throws Exception
     */
    @PostMapping("/comboProductDetail")
    public ResponseResult comboProductDetail(String userId, String productId) throws Exception {
        if (userId == null || userId.equals("") || productId == null || productId.equals(""))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        List<ProductEntity> productEntities = productService.comboProductDetail(userId, productId);
        return new ResponseResult(ResponseMessage.OK, productEntities);
    }

    /**
     * 用户咨询价格(申请成为vip)
     *
     * @param applyVip
     * @return
     * @throws Exception
     */
    @PostMapping("/advisoryProductPrice")
    public ResponseResult advisoryProductPrice(@RequestBody ApplyVipEntity applyVip) throws Exception {
        if (applyVip == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return productService.advisoryProductPrice(applyVip);
    }

    /**
     * 查询用户购物车数量
     *
     * @param userId
     * @throws Exception
     * @countCartProduct
     */
    @PostMapping("/countCartProduct")
    public ResponseResult countCartProduct(String userId) throws Exception {
        if (userId == null || userId == "")
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return cartService.countCartProduct(userId); 
    }

    /**
     * 展示用户的购物车数据
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/userCartList")
    public ResponseResult userCartList(String userId,String orderInfoId) throws Exception {
        if (userId == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return cartService.userCartList(userId,orderInfoId);
    }

    /**
     * 用户添加商品到购物车
     *
     * @param addUserCartModel
     * @return
     * @throws Exception
     */
    @PostMapping("/addUserCart")
    public ResponseResult addUserCart(@RequestBody List<AddUserCartModel> addUserCartModel) throws Exception {
        if (addUserCartModel == null )
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return cartService.addUserCart(addUserCartModel);
    }

    /**
     * 用户修改购物车商品规格数量和选中状态
     *
     * @param cartProduct
     * @return
     * @throws Exception
     */
    @PostMapping("/updateCartStatus")
    public ResponseResult updateCartStatus(@RequestBody CartProductEntity cartProduct) throws Exception {
        if (cartProduct == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return cartService.updateCartBuyNum(cartProduct);
    }

    /**
     * 用户删除购物车商品数据
     *
     * @param cartId
     * @return
     * @throws Exception
     */
    @PostMapping("/delUserCartRemark")
    public ResponseResult delUserCartRemark(Integer cartId) throws Exception {
        if (cartId == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return cartService.delUserCartRemark(cartId);
    }

    /**
     * 删除换购商品
     *
     * @param cartProductId
     * @return
     * @throws Exception
     */
    @PostMapping("/delTradProduct")
    public ResponseResult delTradProduct(Integer cartProductId) throws Exception {
        if (cartProductId == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return cartService.delTradProduct(cartProductId);
    }

    /**
     * 确认订单
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/verifyOrderInfo")
    public ResponseResult verifyOrderInfo(String userId,String orderInfoId) throws Exception {
        if (userId == null || userId == "")
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return productService.verifyOrderInfo(userId,orderInfoId);
    }

    /**
     * 保存订单
     *
     * @param orderInfoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/saveOrderInfo")
    public synchronized ResponseResult saveOrderInfo(@RequestBody OrderInfoEntity orderInfoEntity) throws Exception {
        if (orderInfoEntity == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return productService.saveOrderInfo(orderInfoEntity);
    }

    /**
     * 微信支付
     * 老项目中搬过来的，参数名，注释不规范懒得改
     *
     * @param request
     * @param response
     * @param orderNumber
     * @param realityMoney
     * @return
     * @throws Exception
     */
    @PostMapping("/wxPay")
    public ResponseResult wxPay(HttpServletRequest request, HttpServletResponse response, String orderNumber, String realityMoney) throws Exception {
        return productService.wxPay(request, response, orderNumber, realityMoney);
    }

    /**
     * 微信支付回调
     *
     * @param request
     * @return
     */
    @PostMapping("/wxBackUrl")
    public ResponseResult wxPayNotify(HttpServletRequest request) throws Exception {
        return productService.wxPayNotify(request);
    }

    /**
     * 支付宝支付
     * 老项目中搬过来的（搬砖工程）
     *
     * @param orderNumber
     * @param realityMoney
     * @return
     * @throws Exception
     */
    @PostMapping("/alipay")
    public ResponseResult alipay(String orderNumber, String realityMoney) throws Exception {
        return productService.alipay(orderNumber, realityMoney);
    }

    /**
     * 支付宝支付回调
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/alipayBackUrl")
    public ResponseResult alipayNotifyUrl(HttpServletRequest request) throws Exception {
        return productService.alipayNotifyUrl(request);
    }

    /**
     * 银联支付
     *
     * @param payMoney
     * @param orderNumber
     * @return
     * @throws Exception
     */
    @PostMapping("/unionPay")
    public ResponseResult unionPay(String realityMoney, String orderNumber) throws Exception {
        if (realityMoney == null || ("").equals(realityMoney) || orderNumber == null || ("").equals(orderNumber)) {
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        }
        return productService.unionPay(realityMoney, orderNumber);
    }

    /**
     * 银联查询订单
     *
     * @param txnTime
     * @param orderNumber
     * @return
     * @throws Exception
     */
    @PostMapping("/queryUnionOrder")
    public ResponseResult queryUnionOrder(String txnTime, String orderNumber) throws Exception {
        if (txnTime == null || ("").equals(txnTime) || orderNumber == null || ("").equals(orderNumber)) {
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        }
        return productService.queryUnionOrder(txnTime, orderNumber);
    }

    /**
     * 银联回调
     *
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @PostMapping("/unionBackUrl")
    public ResponseResult unionBackUrl(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return productService.unionBackUrl(req, resp);
    }

    /**
     * 积分商品列表和详情
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/showListIntegral")
    public ResponseResult showListIntegral(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, Integer integralId, String userId) throws Exception {
        return integralService.showListIntegral(pageNum, pageSize, integralId, userId);
    }

    /**
     * 兑换积分商品
     *
     * @param userId
     * @param tradNum
     * @param integralId
     * @return
     * @throws Exception
     */
    @PostMapping("/tradIntegralShop")
    public ResponseResult tradIntegralShop(String userId, Integer tradNum, Integer integralId) throws Exception {
        return integralService.tradIntegralShop(userId, tradNum, integralId);
    }

    /**
     * 用户积分兑换记录
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/integralRecord")
    public ResponseResult integralRecord(String userId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        if (userId == null || ("").equals(userId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return integralService.integralRecord(userId, pageNum, pageSize);
    }

    /**
     * 商品分享
     *
     * @param userId
     * @param productId
     * @return
     * @throws Exception
     */
    @PostMapping("/productShare")
    public ResponseResult productShare(String userId, String productId, String shareAddress) throws Exception {
        if (userId == null || ("").equals(userId) || productId == null || ("").equals(productId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return productService.productShare(userId, productId, shareAddress);
    }
}
